package while循环;

import com.sun.security.jgss.GSSUtil;

import java.util.Scanner;

/*
       声明变量balance并初始化为0，用以表示银行账户的余额，下面通过ATM机程序实现存款，取款等功能。
       =========ATM========
          1、存款
          2、取款
          3、显示余额
          4、退出
       请选择(1-4)：
        */
//余额
public class Demo7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int balance=0;
        boolean flag=true;
        do {
            System.out.println(" =========ATM========");
            System.out.println("1.存款");
            System.out.println("2.取款");
            System.out.println("3.显示余额");
            System.out.println("4.退出");
            System.out.println("请选择(1-4):");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入你要存款的余额:");
                    int money = scanner.nextInt();
                    balance += money;
                    break;
                case 2:
                    System.out.println("请输入你要取走多少钱:");
                    int money1 = scanner.nextInt();
                    if (money1>balance){
                        System.out.println("余额不足");
                    }else {
                        balance -= money1;
                    }
                    break;
                case 3:
                    System.out.println("显示余额");
                    System.out.println("您当前的余额为："+balance);
                    break;
                case 4:
                    System.out.println("您确定要退出嘛(y/n)");
                    char answer = scanner.next().charAt(0);
                    if (answer == 'y') {
                        flag = false;
                    } else if (answer=='n') {
                        flag = true;
                    }
                    break;
                default:
                    System.out.println("没有与之相匹配的选项");
                    break;
            }

        }while (flag);
    }
}
